package controller;

import java.awt.event.*;
import javax.swing.*;

import model.AppointmentModel;
import view.AppointmentSearchView;

/**
 * 12-01-2010
 * 
 * This class is the controller for searching the results for the search view.
 * Must only be added as actionlistener on the mainview search button,
 * since it calls the mainview search textfield.
 * 
 * @author Maarten Ezendam & Erwin Cavas
 */
public class AppointmentSearchController implements ActionListener {

	private AppointmentSearchView appointmentSearchView;
	private AppointmentController appointmentController;
	private JTextField mainViewSearchField;

	/**
	 * Default constructor.
	 * 
	 * @param newController
	 * @param newSearchfield
	 */
	public AppointmentSearchController(AppointmentController newController, JTextField newSearchfield) {
		appointmentSearchView = new AppointmentSearchView(this);
		appointmentController = newController;
		mainViewSearchField = newSearchfield;
	}

	/**
	 * Not to be called manually
	 */
	public void actionPerformed(ActionEvent e) {
		queryModelAndReturnToView(mainViewSearchField.getText());
	}

	/**
	 * This will get the query with the results that have been searched.
	 * 
	 * @param query
	 */
	public void queryModelAndReturnToView(String query) {
		appointmentSearchView.fillListWithResults(appointmentController.searchAppointments(query));
		appointmentSearchView.setSearchString(query);
	}

	/**
	 * With this you will be able to edit the searched appointment.
	 * 
	 * @param id
	 */
	public void editAppointment(int id) {
		appointmentController.editAppointment(id);
	}

	/**
	 * This is the observer for the searchview.
	 * 
	 * @param model
	 */
	public void addModelForObserving(AppointmentModel model) {
		model.addObserver(appointmentSearchView);
	}
}